<?php

namespace App\Http\Controllers\Mobile;


use App\Models\UserAddress;
use App\Repos\UserRepo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserAddressController extends Controller
{

    public function addressCreate(Request $request)
    {
        $wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);
        $userId = $user->id;
        return view('mobile.address',compact('userId'));
    }

    public function addressStore(Request $request)
    {

        $wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);



        $data = $request->except(['_token']);

        if($data['is_default']==1) {
            $where['user_id'] = $user->id;
            UserAddress::where($where)->update(['is_default'=>0]);
        }
        //todo 校验

        $flag = UserAddress::create($data);
        if($flag){
            $rs['status']      = 'success';
            $rs['data']        = $flag;
        }

        return response()->json($rs);
    }

    public function index(Request $request)
    {

        $wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);
        $where['user_id'] = $user->id;
        $addressList = UserAddress::where($where)->orderBy('is_default','desc')->paginate(10);

    }
}
